<div class="box span12">
    <div class="box-header">
        <h2>
            <i class="icon-envelope"></i>
            Notifications 
        </h2>
    </div>
    <div class="box-content">
        <ul class="nav tab-menu nav-tabs" id="myTab">
            <li class="active"><a href="#email">Email</a></li>
            <li><a href="#sms">GMT SMS</a></li>
        </ul>
        <div id="myTabContent" class="tab-content">
            <div class="tab-pane active" id="email">
                <?php if (Yii::app()->user->hasFlash('form_success')): ?>
                    <div class="alert alert-success">
                        <button type="button" class="close" data-dismiss="alert">×</button>
                        <strong>All done!</strong>
                        <?php echo Yii::app()->user->getFlash('form_success'); ?>
                    </div>
                <?php elseif (Yii::app()->user->hasFlash('form_error')): ?>
                    <div class="alert alert-error">
                        <button type="button" class="close" data-dismiss="alert">×</button>
                        <strong>Oh snap!</strong> 
                        <?php echo Yii::app()->user->getFlash('form_error'); ?>
                    </div>
                <?php endif; ?>        

                <?php foreach ($emailSettings as $emailModel): ?>
                    <?php
                    $this->renderPartial('_email', array(
                        'model' => $emailModel
                    ));
                    ?>
                <?php endforeach; ?>
            </div>
            <div class="tab-pane" id="sms">
                <form class="form-horizontal" action="" onsubmit="return submit_sms_setting()">
                    <fieldset>
                        <h3>Account Setting</h3>
                        <br>
                        <div class="control-group">
                            <label for="usr_name" class="control-label">Registered User Name</label>
                            <div class="controls">
                                <input type="text" id="usr_name" 
                                       value="<?php echo (isset($smsSetting->username)) ? $smsSetting->username : '' ?>" class="input-xlarge focused">
                            </div>
                        </div>
                        <div class="control-group">
                            <label for="passw" class="control-label">Password</label>
                            <div class="controls">
                                <input type="password" id="passw"
                                       value="<?php echo isset($smsSetting->password) ? $smsSetting->password : '' ?>" 
                                       class="input-xlarge focused">
                            </div>
                        </div>
                        <hr><br /><br />
                        <h3>Message Settings</h3>
                        <br>
                        <div class="control-group">
                            <label for="message_1" class="control-label">Message No1</label>
                            <div class="controls">
                                <div class="wrap-message">
                                    <span class="label label-info pull-right ms-character">Character <span id="count_msg_1">0</span></span>
                                    <span class="label label-warning pull-right">(Maximum 160)</span>
                                    <textarea id="message_1" spellcheck="false" 
                                              onkeydown="return keypress(event, 1);" onkeyup="return keypress(event, 1);" 
                                              class="message-area"><?php echo isset($smsSetting->msg_1) ? $smsSetting->msg_1 : '' ?></textarea>
                                </div>
                            </div>
                        </div>
                        <div class="control-group">
                            <label for="message_2" class="control-label">Message No2</label>
                            <div class="controls">
                                <div class="wrap-message">
                                    <span class="label label-info pull-right ms-character">Character <span id="count_msg_2">0</span></span>
                                    <span class="label label-warning pull-right">(Maximum 160)</span>
                                    <textarea id="message_2" spellcheck="false" 
                                              onkeydown="return keypress(event, 2);" 
                                              onkeyup="return keypress(event, 2);" 
                                              class="message-area"><?php echo isset($smsSetting->msg_2) ? $smsSetting->msg_2 : '' ?></textarea>
                                </div>
                            </div>
                        </div>
                        <div class="control-group">
                            <label for="message_3" class="control-label">Message No3</label>
                            <div class="controls">
                                <div class="wrap-message">
                                    <span class="label label-info pull-right ms-character">Character <span id="count_msg_3">0</span></span>
                                    <span class="label label-warning pull-right">(Maximum 160)</span>
                                    <textarea id="message_3" spellcheck="false" 
                                              onkeydown="return keypress(event, 3);" 
                                              onkeyup="return keypress(event, 3);" 
                                              class="message-area"><?php echo isset($smsSetting->msg_3) ? $smsSetting->msg_3 : '' ?></textarea>
                                </div>
                            </div>
                        </div>
                        <hr >
                        <button class="btn btn-primary" type="submit">Save changes</button>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>
</div><!--/span-->
<script type="text/javascript">
    function keypress(e, message) {
        if (window.event) {
            keypressed = window.event.keyCode; //IE
        } else {
            keypressed = e.which; //NON-IE, Standard
        }
        var str_msg = $("#message_" + message).val();
        str_msg = remove_char(str_msg);
        $("#count_msg_" + message).empty().append(str_msg.length);
        if (keypressed != 8)
            if (str_msg.length >= 160)
                return false;
    }
    function remove_char(str) {
        str = str.split(" ").join("");
        return str;
    }
    function IsNumeric(sText)
    {
        var ValidChars = "0123456789.";
        var IsNumber = true;
        var Char;
        for (i = 0; i < sText.length && IsNumber == true; i++)
        {
            Char = sText.charAt(i);
            if (ValidChars.indexOf(Char) == -1)
                IsNumber = false;
        }
        return IsNumber;
    }
    function submit_sms_setting() {
        if (document.getElementById("usr_name")) {
            if (document.getElementById("usr_name").value == '') {
                alert('Input Registered User Name.');
                document.getElementById("usr_name").focus();
                return false;
            }
        }
        if (document.getElementById("passw")) {
            if (document.getElementById("passw").value == '') {
                alert('Input Password.');
                document.getElementById("passw").focus();
                return false;
            }
        }
        $.post("<?php echo Yii::app()->createUrl('setting/editSms') ?>", {
            update_sms_cf: 'yes',
            usr_name: $("#usr_name").val(),
            passw: $("#passw").val(),
            message_1: $("#message_1").val(),
            message_2: $("#message_2").val(),
            message_3: $("#message_3").val()
        }, function(data) {
            if (data === 'ok') {
                alert('Save success.');
            } else {
                alert('Save error.');
            }
        });

        return false;
    }
    function count_character(message) {
        var str_msg = $("#message_" + message).val();
        str_msg = remove_char(str_msg);
        $("#count_msg_" + message).empty().append(str_msg.length);
    }
    $(function() {
        count_character(1);
        count_character(2);
        count_character(3);
    })

    $(document).ready(function() {
        var $target = $('.btn-minimize').parent().parent().next('.box-content');
        $('i', $('.btn-minimize')).removeClass('icon-chevron-up').addClass('icon-chevron-down');
        $target.slideToggle();
    });
</script>
